package logic

import (
	"context"
	"database/sql"
	"time"

	"backend/apps/book/models"
	"backend/apps/book/proto"
	"backend/apps/book/rpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CreateAuthorLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCreateAuthorLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateAuthorLogic {
	return &CreateAuthorLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// CreateAuthor 作者相关操作
func (l *CreateAuthorLogic) CreateAuthor(in *proto.Author) (*proto.CommonResponse, error) {
	now := time.Now().Unix()
	_, err := l.svcCtx.AuthorModel.Insert(l.ctx, &models.Author{
		Name:      in.Name,
		AvatarUrl: in.AvatarUrl,
		Introduction: sql.NullString{
			String: in.Introduction,
			Valid:  true,
		},
		CreateTime: now,
		UpdateTime: now,
	})
	return &proto.CommonResponse{}, err
}
